<template>
  <div class="app-container">
    <el-form :inline="true" :model="searchFrom" class="demo-form-inline" size="small">
      <el-form-item>
        <el-input v-model="searchFrom.name" placeholder="用户名称"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column align="center" label="编号" width="80">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="用户头像" width="110" align="center">
        <template slot-scope="scope">
          <el-avatar
            shape="square"
            :size="40"
            fit="fit"
            :src="scope.row.userType === 0 ? scope.row.icon : scope.row.restaurantPhoto"
          />
        </template>
      </el-table-column>
      <el-table-column label="饭店名称" align="center">
        <template slot-scope="scope">
          {{ scope.row.restaurantName }}
        </template>
      </el-table-column>
      <el-table-column label="用户昵称" align="center">
        <template slot-scope="scope">
          {{ scope.row.username }}
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="发放粮店" align="center">
        <template slot-scope="scope">
          {{ scope.row.shopName }}
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="类型" width="110" align="center">
        <template slot-scope="scope">
          <el-tag>{{ scope.row.userType | typeFilter }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="领取状态" width="110" align="center">
        <template slot-scope="scope">
          <el-tag>{{ scope.row.getType | getTypeFilter }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="使用状态" width="110" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.useStatus | useStatusProgress">{{ scope.row.useStatus | useStatus }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="发放时间" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time"/>
          <span>{{ scope.row.getTime }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="120">
        <template slot-scope="scope">
          <el-button v-if="scope.row.useStatus === 0" type="danger" size="mini" @click="use(scope.row)">使用</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="app-pagination">
      <el-pagination
        layout="total, sizes, prev, pager, next, jumper"
        :current-page="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="limit"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        @prev-click="handlePrevChange"
        @next-click="handleNextChange"
      />
    </div>
  </div>
</template>

<script>

import { getCouponUserList, useCouponUser } from '@/api/SmsUser'

export default {
  filters: {
    typeFilter(status) {
      const statusMap = {
        0: '个人',
        1: '饭店'
      }
      return statusMap[status]
    },
    getTypeFilter(status) {
      const statusMap = {
        0: '未领取',
        1: '已领取'
      }
      return statusMap[status]
    },
    useStatusProgress(status) {
      const statusMap = {
        0: 'primary',
        1: 'danger',
        2: 'primary'
      }
      return statusMap[status]
    },
    useStatus(status) {
      const statusMap = {
        0: '未使用',
        1: '已使用',
        2: '已过期'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      searchFrom: {
        name: '',
        number: ''
      },
      list: null,
      listLoading: true,
      total: 0,
      limit: 10,
      page: 1
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      const searchParams = this.searchFrom
      searchParams['page'] = this.page
      searchParams['limit'] = this.limit
      getCouponUserList(searchParams).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    onSearch() {
      this.fetchData()
    },
    /* 上一页*/
    handlePrevChange(event) {
      this.page = event
      this.fetchData()
    },
    /* 下一页*/
    handleNextChange(event) {
      this.page = event
      this.fetchData()
    },
    /* 数字点击*/
    handleCurrentChange(event) {
      this.page = event
      this.fetchData()
    },
    handleSizeChange(event) {
      this.limit = event
      this.fetchData()
    },
    use(event) {
      this.$confirm('此操作将用户该优惠券设为已使用, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        useCouponUser({ id: event.id }).then(res => {
          if (res.code === 200) {
            this.$message.success('设置成功')
            this.fetchData()
          } else {
            this.$message.error('网络错误')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    }
  }
}
</script>
